﻿using System.Windows;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Recipes.Chapter01.Lesson02
{
    public class Camera : GameComponent
    {
        protected Matrix _View;
        protected Matrix _Projection;

        public Matrix View
        {
            get
            {
                return _View;
            }
        }

        public Matrix Projection
        {
            get
            {
                return _Projection;
            }
        }

        private Vector3 _cameraPosition;
        private Vector3 _cameraDirectionVector;
        private Vector3 _cameraUpVector;
        private Vector3 _cameraRightVector;
        private Vector3 _cameraBackwardVector;

        private Matrix _rotateByUpVector;
        private Matrix _rotateByUpVectorReverse;
        private Matrix _rotateByRightVector;
        private Matrix _rotateByRightVectorReverse;
        private Matrix _rotateByBackwardVector;
        private Matrix _rotateByBackwardVectorReverse;

        public Camera(Game game, Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) : base(game)
        {
            game.Window.SizeChanged += WindowOnSizeChanged;
            _cameraPosition = cameraPosition;
            _cameraDirectionVector = cameraTarget - cameraPosition;
            _cameraDirectionVector.Normalize();
            _cameraUpVector = cameraUpVector;
            _cameraUpVector.Normalize();
            _cameraRightVector = Vector3.Cross(_cameraDirectionVector, _cameraUpVector);
            _cameraRightVector.Normalize();
            _cameraBackwardVector = Vector3.Cross(_cameraRightVector, _cameraUpVector);
            _cameraBackwardVector.Normalize();

            _rotateByUpVector = Matrix.CreateFromAxisAngle(_cameraUpVector, MathHelper.PiOver4 / 60);
            _rotateByUpVectorReverse = Matrix.CreateFromAxisAngle(_cameraUpVector, -MathHelper.PiOver4 / 60);
            _rotateByRightVector = Matrix.CreateFromAxisAngle(_cameraRightVector, MathHelper.PiOver4 / 60);
            _rotateByRightVectorReverse = Matrix.CreateFromAxisAngle(_cameraRightVector, -MathHelper.PiOver4 / 60);
            _rotateByBackwardVector = Matrix.CreateFromAxisAngle(_cameraBackwardVector, MathHelper.PiOver4 / 60);
            _rotateByBackwardVectorReverse = Matrix.CreateFromAxisAngle(_cameraBackwardVector, -MathHelper.PiOver4 / 60);

            _View = Matrix.CreateLookAt(cameraPosition, _cameraPosition + _cameraDirectionVector, cameraUpVector);
        }

        private void WindowOnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            _Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float) (e.NewSize.Width / e.NewSize.Height), 1.0f, 100f);
        }

        public override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.A))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByUpVector);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            if (keyState.IsKeyDown(Key.D))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByUpVectorReverse);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            if (keyState.IsKeyDown(Key.W))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByRightVector);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            if (keyState.IsKeyDown(Key.S))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByRightVectorReverse);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            if (keyState.IsKeyDown(Key.Q))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByBackwardVector);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            if (keyState.IsKeyDown(Key.E))
            {
                _cameraDirectionVector = Vector3.Transform(_cameraDirectionVector, _rotateByBackwardVectorReverse);
                _cameraDirectionVector.Normalize();
                _View = Matrix.CreateLookAt(_cameraPosition, _cameraPosition + _cameraDirectionVector, _cameraUpVector);
            }
            base.Update(gameTime);
        }
    }
}